﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Contract
{
    public partial class frmOutContract : Form
    {
        Entities db = new Entities();

        T_OuterCtr m_OuterCtr = null;

        T_InnerCtr m_InnerCtr=null;

        private bool IsQuery = true;

        private int m_oID = 0;

        List<int> changeList = new List<int>();

        List<T_OuterCtr> tOrder = null;

        public List<int> id
        {
            set
            {
                tOrder = new List<T_OuterCtr>();
                for (int i = 0; i < value.Count; i++)
                {
                    int id = value[i];
                    var list = db.T_OuterCtr.Where(a => a.oID == id);

                    tOrder.Add(list.FirstOrDefault());
                }
            }
        }

        public int oID
        {
            set
            {
                var list = db.T_OuterCtr.Where(a => a.oID == value);
                foreach (var outerctr in list)
                {
                    m_OuterCtr = outerctr;
                    break;
                }
                if (m_OuterCtr == null) return;

                txtCtrNo.Text = m_OuterCtr.oCtrNo;
                dtoCtrDate.ValueText = m_OuterCtr.oCtrDate.ToString();
                txtoSignAt.Text = m_OuterCtr.oSignAt;
                txtfob.Text = m_OuterCtr.fob;
                cboCustom.Text = m_OuterCtr.custom;
                cbfinalCustom.Text = m_OuterCtr.finalCustom;
                dtconfirmDate.ValueText = m_OuterCtr.confirmDate.ToString();
                cbSeason.Text = m_OuterCtr.Season;

                dtShipDate1.ValueText = m_OuterCtr.shipDate1.ToString();
                dtShipDate2.ValueText = m_OuterCtr.shipDate2.ToString();
                cbbOrigin.Text = m_OuterCtr.origin;
                cbbShipPort.Text = m_OuterCtr.shipPort;
                if (m_OuterCtr.partShip == "是") rbship1.Checked = true;
                if (m_OuterCtr.tranShip == "是") rbzship1.Checked = true;
                txtBNumber.Text = m_OuterCtr.totalBNumber.ToString();
                txtyName.Text = m_OuterCtr.yName;
                txtsGroup.Text = m_OuterCtr.sGroup;
                cbbPayment.Text = m_OuterCtr.payment;
                dtLCTime.ValueText = m_OuterCtr.lcTime.ToString();
                dtlcFactTime.ValueText = m_OuterCtr.lcFactTime.ToString();
                txtLCNo.Text = m_OuterCtr.lcNo;
                //字段待考察
                cbbLC.Text = m_OuterCtr.ac;
                txtLCTTDesc.Text = m_OuterCtr.lcttDesc;

                var detailList = db.T_CtrDetail1.Where(a => a.oID == value);
                gv1.DataSource = detailList.ToList();

                var samplelist = db.T_CtrSampleDetail.Where(a => a.xID == value);
                gv5.DataSource = samplelist.ToList();
            }
        }
        public frmOutContract()
        {
            InitializeComponent();
            gvDataSource();
            panList.BringToFront();
        }

        private void GetBackInfo()
        {
            Service.Cust(cboCustom);
            Service.CustDept(cbfinalCustom);
            //Service.ODescript(cboDescript);
            Service.Season(cbSeason);
            Service.Origin(cbbOrigin);
            Service.ShipPort(cbbShipPort);
            Service.DstPost(cbbDstPost);
            //Service.YName(txtyName);
            Service.Group(txtsGroup);
            Service.LC(cbbLC);
            //Service.OCtrNo(cbbQryCtrNo);
            Service.Buyer(txtyName);
        }

        public void gvDataSource()
        {
            var list = db.T_OuterCtr.Where(a=>a.state.Trim()!="已生成").ToList();
            bslist.DataSource = list;
            bnavlist.BindingSource = bslist;
        }

        private void gvID()
        {
            if (gvlist.RowCount > 0)
            {
                int index = gvlist.CurrentRow.Index;
                oID = Convert.ToInt32(gvlist.Rows[index].Cells["coloID"].Value.ToString());
                m_oID = Convert.ToInt32(gvlist.Rows[index].Cells["coloID"].Value.ToString());
            }
            else
            {
                TxtClear();
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            gvID();
            if (this.IsQuery)
            {
                this.panDetail.BringToFront();
                this.btn1.Text = "概要";
                this.IsQuery = false;

            }
            else
            {
                this.panList.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void gvlist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.oID = Convert.ToInt32(gvlist.Rows[e.RowIndex].Cells["coloID"].Value.ToString());
                this.panDetail.BringToFront();
                this.btn1.Text = "概要";
                this.IsQuery = false;
            }
            catch
            {
                Msg.ShowError("请选择表里的数据！");
            }
        }

        private void TxtClear()
        {
            Helper.TxtClear(txtCtrNo);
            dtoCtrDate.ValueText = "";
            Helper.TxtClear(txtoSignAt);
            Helper.TxtClear(txtfob);
            Helper.CBClear(cboCustom);
            Helper.CBClear(cbfinalCustom);
            dtconfirmDate.ValueText = "";
            Helper.CBClear(cbSeason);

            dtShipDate1.ValueText = "";
            dtShipDate2.ValueText = "";
            Helper.CBClear(cbbOrigin);
            Helper.CBClear(cbbShipPort);
            rbship1.Checked = true;
            rbzship1.Checked = true;
            Helper.TxtClear(txtBNumber);
            Helper.CBClear(txtyName);
            Helper.CBClear(txtsGroup);
            Helper.CBClear(cbbPayment);
            dtLCTime.ValueText = "";
            dtlcFactTime.ValueText = "";
            Helper.TxtClear(txtLCNo);
            //字段待考察
            Helper.CBClear(cbbLC);
            Helper.TxtClear(txtLCTTDesc);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            m_OuterCtr = null;
            this.panDetail.BringToFront();
            this.btn1.Text = "概要";
            this.IsQuery = false;


            TxtClear();
        }

        private void SaveAdd()
        {
            m_OuterCtr = new T_OuterCtr();

            m_OuterCtr.oID = db.T_OuterCtr.OrderByDescending(a => a.oID).First().oID + 1;
            m_OuterCtr.oCtrNo = txtCtrNo.Text;
            if (!string.IsNullOrEmpty(dtoCtrDate.ValueText)) m_OuterCtr.oCtrDate = Convert.ToDateTime(dtoCtrDate.ValueText);
            m_OuterCtr.oSignAt = txtoSignAt.Text;
            m_OuterCtr.fob = txtfob.Text;
            m_OuterCtr.custom = cboCustom.Text;
            m_OuterCtr.finalCustom = cbfinalCustom.Text;
            if (!string.IsNullOrEmpty(dtconfirmDate.ValueText)) m_OuterCtr.confirmDate = Convert.ToDateTime(dtconfirmDate.ValueText);
            m_OuterCtr.Season = cbSeason.Text;

            if (!string.IsNullOrEmpty(dtShipDate1.ValueText)) m_OuterCtr.shipDate1 = Convert.ToDateTime(dtShipDate1.ValueText);
            if (!string.IsNullOrEmpty(dtShipDate2.ValueText)) m_OuterCtr.shipDate2 = Convert.ToDateTime(dtShipDate2.ValueText);
            m_OuterCtr.origin = cbbOrigin.Text;
            m_OuterCtr.shipPort = cbbShipPort.Text;
            if (rbship1.Checked) m_OuterCtr.partShip = "是";
            else if (rbship2.Checked) m_OuterCtr.partShip = "否";
            if (rbzship1.Checked) m_OuterCtr.tranShip = "是";
            else if (rbzship2.Checked) m_OuterCtr.tranShip = "否";
            if (!string.IsNullOrEmpty(txtBNumber.Text)) m_OuterCtr.totalBNumber = Convert.ToInt32(txtBNumber.Text);
            m_OuterCtr.yName = txtyName.Text;
            m_OuterCtr.sGroup = txtsGroup.Text;
            m_OuterCtr.payment = cbbPayment.Text;
            if (!string.IsNullOrEmpty(dtLCTime.ValueText)) m_OuterCtr.lcTime = Convert.ToDateTime(dtLCTime.ValueText);
            if (!string.IsNullOrEmpty(dtlcFactTime.ValueText)) m_OuterCtr.lcFactTime = Convert.ToDateTime(dtlcFactTime.ValueText);
            txtLCNo.Text = m_OuterCtr.lcNo;
            //字段待考察
            m_OuterCtr.ac = cbbLC.Text;
            m_OuterCtr.lcttDesc = txtLCTTDesc.Text;
            m_OuterCtr.state = "未生成";

            db.T_OuterCtr.Add(m_OuterCtr);
            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void SaveModify()
        {
            if (m_OuterCtr == null) return;

            m_OuterCtr.oCtrNo = txtCtrNo.Text;
            if (!string.IsNullOrEmpty(dtoCtrDate.ValueText)) m_OuterCtr.oCtrDate = Convert.ToDateTime(dtoCtrDate.ValueText);
            m_OuterCtr.oSignAt = txtoSignAt.Text;
            m_OuterCtr.fob = txtfob.Text;
            m_OuterCtr.custom = cboCustom.Text;
            m_OuterCtr.finalCustom = cbfinalCustom.Text;
            if (!string.IsNullOrEmpty(dtconfirmDate.ValueText)) m_OuterCtr.confirmDate = Convert.ToDateTime(dtconfirmDate.ValueText);
            m_OuterCtr.Season = cbSeason.Text;

            if (!string.IsNullOrEmpty(dtShipDate1.ValueText)) m_OuterCtr.shipDate1 = Convert.ToDateTime(dtShipDate1.ValueText);
            if (!string.IsNullOrEmpty(dtShipDate2.ValueText)) m_OuterCtr.shipDate2 = Convert.ToDateTime(dtShipDate2.ValueText);
            m_OuterCtr.origin = cbbOrigin.Text;
            m_OuterCtr.shipPort = cbbShipPort.Text;
            if (rbship1.Checked) m_OuterCtr.partShip = "是";
            else if (rbship2.Checked) m_OuterCtr.partShip = "否";
            if (rbzship1.Checked) m_OuterCtr.tranShip = "是";
            else if (rbzship2.Checked) m_OuterCtr.tranShip = "否";
            if (!string.IsNullOrEmpty(txtBNumber.Text)) m_OuterCtr.totalBNumber = Convert.ToInt32(txtBNumber.Text);
            m_OuterCtr.yName = txtyName.Text;
            m_OuterCtr.sGroup = txtsGroup.Text;
            m_OuterCtr.payment = cbbPayment.Text;
            if (!string.IsNullOrEmpty(dtLCTime.ValueText)) m_OuterCtr.lcTime = Convert.ToDateTime(dtLCTime.ValueText);
            if (!string.IsNullOrEmpty(dtlcFactTime.ValueText)) m_OuterCtr.lcFactTime = Convert.ToDateTime(dtlcFactTime.ValueText);
            txtLCNo.Text = m_OuterCtr.lcNo;
            //字段待考察
            m_OuterCtr.ac = cbbLC.Text;
            m_OuterCtr.lcttDesc = txtLCTTDesc.Text;

            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btn1_Click(null, null);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信保存订单信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                if (m_OuterCtr == null)
                {
                    if (db.T_OuterCtr.Where(a => a.oCtrNo == txtCtrNo.Text).Count() > 0)
                    {
                        Msg.ShowWarning("已存在的合同号，请重新输入！");
                        return;
                    }
                    SaveAdd();
                    gvDataSource();
                    this.panList.BringToFront();
                    this.btn1.Text = "明细";
                    this.IsQuery = true;
                }
                else
                {
                    SaveModify();
                    gvDataSource();
                    this.panList.BringToFront();
                    this.btn1.Text = "明细";
                    this.IsQuery = true;
                }
            }
        }

        private void btnDelOuterCtr_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信删除信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                gvID();

                if (m_oID == 0)
                {
                    Msg.ShowError("请选择一条记录！");
                    return;
                }
                T_OuterCtr outctr = db.T_OuterCtr.First(a => a.oID == m_oID);
                db.T_OuterCtr.Remove(outctr);
                int i = db.SaveChanges();
                Msg.ShowInfo(string.Format("已删除{0}条订单信息。", i));
                gvDataSource();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("尚未保存，您确定要取消吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                this.panList.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void btnApplyInCtr_Click(object sender, EventArgs e)
        {
            if (changeList != null)
            {
                this.id = changeList;
                int i = 0;
                foreach (var oidlist in tOrder)
                {
                    m_OuterCtr = oidlist;
                    if (m_OuterCtr.state != null)
                    {
                        if (m_OuterCtr.state.Trim() == "已生成")
                        {
                            Msg.ShowWarning("包含已生成订单，请重新选择！");
                            return;
                        }
                    }
                    m_InnerCtr = new T_InnerCtr();

                    m_InnerCtr.iID = db.T_InnerCtr.OrderByDescending(a => a.iID).First().iID + 1;
                    m_InnerCtr.iCtrNo = m_OuterCtr.oCtrNo;
                    m_InnerCtr.iCtrDate = DateTime.Now.Date;
                    m_InnerCtr.opName = Startup.GetApp().CurrentUser.name;


                    db.T_InnerCtr.Add(m_InnerCtr);
                    m_OuterCtr.state = "已生成";

                    db.SaveChanges();
                    i++;



                    var list = db.T_CtrSampleDetail.Where(a => a.xID == m_OuterCtr.oID);
                    foreach (var ctr in list)
                    {
                        ctr.iID = m_InnerCtr.iID;
                        db.SaveChanges();
                    }

                    changeList.Remove(m_OuterCtr.oID);
                }

                Msg.ShowInfo(string.Format("已生成{0}条内合同信息。", i));
                gvDataSource();
            }
        }

        private void gvlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                if (bool.Parse(this.gvlist.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == true)
                {
                    changeList.Add(int.Parse(gvlist.Rows[e.RowIndex].Cells["coloID"].Value.ToString()));
                }
                else if (bool.Parse(this.gvlist.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == false)
                {
                    changeList.Remove(int.Parse(gvlist.Rows[e.RowIndex].Cells["coloID"].Value.ToString()));
                }
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            var list = db.T_OuterCtr.Where(a => a.oCtrNo == txtoCtrNo.Text).ToList();
            bslist.DataSource = list; 
        }

        private void frmOutContract_Load(object sender, EventArgs e)
        {
            GetBackInfo();
        }

    }
}
